package com.jintian.smart.platform.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import com.jintian.smart.base.api.security.provider.ILevel3ProtectConfigProvider;
import com.jintian.smart.base.api.system.constant.SystemSwaggerTagConst;
import com.jintian.smart.base.api.system.form.LoginForm;
import com.jintian.smart.base.api.system.view.CaptchaVO;
import com.jintian.smart.base.api.system.view.LoginResultVO;
import com.jintian.smart.kernel.common.context.ContextHolder;
import com.jintian.smart.kernel.core.annotation.NoNeedLogin;
import com.jintian.smart.kernel.core.constant.RequestHeaderConst;
import com.jintian.smart.kernel.core.domain.ResponseDTO;
import com.jintian.smart.platform.system.service.LoginService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 员工登录
 */
@RestController
@Tag(name = SystemSwaggerTagConst.SYSTEM_LOGIN)
@RequestMapping("/sys")
public class LoginController {
    @Resource
    private LoginService loginService;

    @DubboReference
    private ILevel3ProtectConfigProvider level3ProtectConfigProvider;

    @NoNeedLogin
    @PostMapping("/login")
    @Operation(summary = "登录")
    public ResponseDTO<LoginResultVO> login(@Valid @RequestBody LoginForm loginForm, HttpServletRequest request) {
        String ip = JakartaServletUtil.getClientIP(request);
        String userAgent = JakartaServletUtil.getHeaderIgnoreCase(request, RequestHeaderConst.USER_AGENT);
        return loginService.login(loginForm, ip, userAgent);
    }

    @GetMapping("/login/getLoginInfo")
    @Operation(summary = "获取登录结果信息")
    public ResponseDTO<LoginResultVO> getLoginInfo() {
        String tokenValue = StpUtil.getTokenValue();
        LoginResultVO loginResult = loginService.getLoginResult(ContextHolder.getUser(), tokenValue);
        loginResult.setToken(tokenValue);
        return ResponseDTO.ok(loginResult);
    }

    @Operation(summary = "退出登录")
    @GetMapping("/login/logout")
    public ResponseDTO<String> logout() {
        return loginService.logout(ContextHolder.getUser());
    }

    @Operation(summary = "获取验证码")
    @GetMapping("/login/getCaptcha")
    @NoNeedLogin
    public ResponseDTO<CaptchaVO> getCaptcha() {
        return loginService.getCaptcha();
    }

    @NoNeedLogin
    @GetMapping("/login/sendEmailCode/{loginName}")
    @Operation(summary = "获取邮箱登录验证码")
    public ResponseDTO<String> sendEmailCode(@PathVariable String loginName) {
        return loginService.sendEmailCode(loginName);
    }


    @NoNeedLogin
    @GetMapping("/login/getTwoFactorLoginFlag")
    @Operation(summary = "获取双因子登录标识")
    public ResponseDTO<Boolean> getTwoFactorLoginFlag() {
        // 双因子登录
        boolean twoFactorLoginEnabled = level3ProtectConfigProvider.isTwoFactorLoginEnabled();
        return ResponseDTO.ok(twoFactorLoginEnabled);
    }
}
